package geppetto.phraseHMM.phraseExtraction.extractedphrase.data;

import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePairData;
import geppetto.reordering.AbstractReorderingCalculator;
import geppetto.reordering.ReorderingInfo;
import gnu.trove.TIntDoubleHashMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


public class ReorderingData extends ExtractedPhrasePairData{

	ReorderingInfo reorderingInfo;
		
	public ReorderingData(ReorderingInfo info) {
		super();
		this.reorderingInfo = info;
	}	

	@Override
	public void merge(ArrayList<HashMap<String, ExtractedPhrasePairData>> generalData, ArrayList<ExtractedPhrasePairData> data) {
		if(data.contains(this)){
			data.remove(this);
		}

		for(ExtractedPhrasePairData d : data){
			if(d instanceof ReorderingData){
				ReorderingData reordData = (ReorderingData)d;
				getReorderingInfo().merge(reordData.getReorderingInfo());
			}
			else{
				System.err.println("trying to merge an instance of LexicalWeightData to with another type of class");
				System.exit(-1);
			}
		}
		ReorderingInfo.compact(reorderingInfo);
		
	}

	@Override
	public String toString() {
		String ret = "";
		for(int key : reorderingInfo.getPreviousWordOrientationCounts().keys()){
			ret += key+":"+reorderingInfo.getPreviousWordOrientationCounts().get(key) + " ";
		}
		ret+="p:n ";
		for(int key : reorderingInfo.getNextWordOrientationCounts().keys()){
			ret += key+":"+reorderingInfo.getNextWordOrientationCounts().get(key) + " ";
		}
		return ret;
	}
	
	public static ReorderingData parseString(String s) {
		String[] split = s.split(" p:n ");
		String[] prevCounts = split[0].split("\\s+");
		String[] netxCoutns = split[1].split("\\s+");
		ReorderingInfo info = new ReorderingInfo();
		for(String ord : prevCounts){
			String[] ordSplit = ord.split(":");
			info.addPrevWordOrientation(Integer.parseInt(ordSplit[0]), Double.parseDouble(ordSplit[1]));
		}
		for(String ord : netxCoutns){
			String[] ordSplit = ord.split(":");
			info.addNextWordOrientation(Integer.parseInt(ordSplit[0]), Double.parseDouble(ordSplit[1]));
		}
		return new ReorderingData(info);
	}

	public ReorderingInfo getReorderingInfo() {
		return reorderingInfo;
	}

	public void setReorderingInfo(ReorderingInfo reorderingInfo) {
		this.reorderingInfo = reorderingInfo;
	}

}
